package com.yulinlin.jdbc.mysql;

import com.yulinlin.lang.util.LambdaUtils;
import com.yulinlin.core.lambda.LambdaPropertyFunction;
import com.yulinlin.core.wrapper.*;

import java.util.Collection;

/**
 * 字符串代理形式
 * @param <E>
 */
public class MysqlLambdaConditionWrapper<E,R> implements ILambdaConditionWrapper<E,R> {



    private IStringConditionWrapper<E,R> conditionWrapper;

    private R endWrapper;

    public MysqlLambdaConditionWrapper( IStringConditionWrapper<E,R> conditionWrapper, R endWrapper) {

        this.conditionWrapper = conditionWrapper;
        this.endWrapper = endWrapper;
    }



    private String lambdaMethodNameToFieldName(LambdaPropertyFunction<?> name){
        return LambdaUtils.lambdaMethodNameToFieldName(name);
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> condition(String sql) {
        conditionWrapper.condition(sql);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> eq(LambdaPropertyFunction<E> name, Object value) {
        conditionWrapper.eq(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> ne(LambdaPropertyFunction<E> name, Object value) {
        conditionWrapper.ne(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> gt(LambdaPropertyFunction<E> name, Number value) {
        conditionWrapper.gt(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> ge(LambdaPropertyFunction<E> name, Number value) {
        conditionWrapper.ge(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> lt(LambdaPropertyFunction<E> name, Number value) {
        conditionWrapper.lt(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> le(LambdaPropertyFunction<E> name, Number value) {
        conditionWrapper.le(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> between(LambdaPropertyFunction<E> name, Object x, Object y) {
        conditionWrapper.between(lambdaMethodNameToFieldName(name),x,y);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> like(LambdaPropertyFunction<E> name, String value) {
        conditionWrapper.like(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> likeLeft(LambdaPropertyFunction<E> name, String value) {
        conditionWrapper.likeLeft(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> likeRight(LambdaPropertyFunction<E> name, String value) {
        conditionWrapper.likeRight(lambdaMethodNameToFieldName(name),value);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> isNull(LambdaPropertyFunction<E> name) {
        conditionWrapper.isNull(lambdaMethodNameToFieldName(name));
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> in(LambdaPropertyFunction<E> name, Collection<?> coll) {
        conditionWrapper.in(lambdaMethodNameToFieldName(name),coll);
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> struct(IStructureWrapper<LambdaPropertyFunction<E>, R> func) {

        func.run(this);

        return null;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> or() {
        conditionWrapper.or();
        return this;
    }

    @Override
    public IAbstractConditionWrapper<LambdaPropertyFunction<E>, R> not() {
        conditionWrapper.not();
        return this;
    }

    @Override
    public R end() {
        return endWrapper;
    }

    @Override
    public String getSql() {
        return conditionWrapper.getSql();
    }

    @Override
    public IParamsWrapper getParams() {
        return conditionWrapper.getParams();
    }
}
